package com.quiz.bis.controller;

import com.quiz.common.core.controller.BaseController;
import com.quiz.common.core.domain.R;
import com.quiz.common.core.domain.entity.SysUser;
import com.quiz.common.mybatis.core.page.PageQuery;
import com.quiz.common.mybatis.core.page.TableDataInfo;
import com.quiz.bis.domain.bo.ExamBo;
import com.quiz.bis.domain.vo.ExamDetailVo;
import com.quiz.bis.domain.vo.ExamVo;
import com.quiz.bis.service.IExamService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 考试活动管理控制器
 *
 * @author ahuan
 */
@RestController
@RequestMapping("/exam/activity")
@Tag(name = "考试活动管理")
@RequiredArgsConstructor
public class ExamController extends BaseController {
    private final IExamService examService;

    /**
     * 分页查询考试活动列表
     *
     * @param pageQuery
     * @param groupId
     * @param type
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo<ExamVo> list(PageQuery pageQuery,
                                      @RequestParam Long groupId,
                                      @RequestParam(required = false) Integer type) {
        return examService.pageExamList(pageQuery, groupId, type);
    }

    /**
     * 分页查询参加考试的人员名单
     *
     * @param pageQuery
     * @param examId
     * @return
     */
    @GetMapping("/student/list")
    public TableDataInfo<SysUser> listStuOfActivity(PageQuery pageQuery,
                                       @RequestParam String examId,
                                       @RequestParam String groupId) {
        return examService.pageJoinExanStudentList(pageQuery, examId, groupId);
    }

    /**
     * 获取考试活动详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<ExamDetailVo> detail(@PathVariable Long id) {
        return R.ok(examService.getExamDetail(id));
    }

    /**
     * 创建考试活动
     *
     * @param examBo
     * @return
     */
    @PostMapping
    public R<Long> create(@Validated @RequestBody ExamBo examBo) {
        return examService.createExam(examBo);
    }

    /**
     * 更新考试活动
     *
     * @param examBo
     * @return
     */
    @PutMapping
    public R<Void> update(@Validated @RequestBody ExamBo examBo) {
        return examService.updateExam(examBo);
    }

    /**
     * 删除考试活动
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R<Void> delete(@PathVariable Long id) {
        return examService.deleteExam(id);
    }

    /**
     * 批量更新活动状态
     *
     * @param ids
     * @param status
     * @return
     */
    @PutMapping("/status")
    public R<Void> updateStatus(@RequestParam List<Long> ids,
                                @RequestParam Integer status) {
        // 只允许发布(1)和撤回(0)操作
        if (status != 0 && status != 1) {
            return R.fail("非法状态值");
        }
        return examService.updateStatusBatch(ids, status);
    }


}